﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB;
using System.Data;
using SkynetMongoDBAccess.Converter;

namespace SkynetMongoDBAccess
{
    // <summary>
    // Mongodb 自带类型（Document）
    // </summary>
    //<remarks>
    // 作者：         李志强
    // 开发时间：  2011-04-18
    // </remarks>
    public class MongoKeyValueDB : MongoAbstractDB
    {
        private MongoCollection<Document> _mongoCollection;
     
        public MongoKeyValueDB(string conString)
            : base(conString)
        {
        }
        public MongoKeyValueDB(string connection, string collection,long cacheLength)
            : base(connection, cacheLength)
        {
            CollectionName = collection;
        }

        public void Open(string dbname, string collname)
        {
            base.Open(dbname);
            if (string.IsNullOrEmpty(collname))
                return;
            if (CollectionName != collname)
            {
                CollectionName = collname;
                _mongoCollection = base.GetDataBase.GetCollection<Document>(collname) as MongoCollection<Document>;
            }
        }

        public MongoCollection<Document> GetMongoCollcetion
        {
            get
            {
                if (_mongoCollection == null)
                {
                    _mongoCollection = base.GetDataBase.GetCollection<Document>(CollectionName) as MongoCollection<Document>;
                }
                return _mongoCollection;
            }
        }

        //插入数据
        public void Insert(Document document)
        {
            GetMongoCollcetion.Insert(document);
        }
        public void Insert(List<Document> list)
        {
            GetMongoCollcetion.Insert(list);
        }
        //保存数据 ,如果不存在则不插入
        public void Save(Document document)
        {
            GetMongoCollcetion.Save(document, false);
        }
        //删除指定条件的数据
        public void Delete(Document document)
        {
            GetMongoCollcetion.Remove(document);
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="document">修改后的数据</param>
        /// <param name="filter">条件判断</param>
        public void Modify(Document document, Document filter)
        {
            GetMongoCollcetion.FindAndModify(document, filter);
        }

        /// <summary>
        /// 删除所有记录
        /// </summary>
        public void DeleteAll()
        {
            GetMongoCollcetion.Remove(new Document() { });
        }
        /// <summary>
        /// 超找所有记录
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Document> FindAll()
        {
            IEnumerable<Document> list = GetMongoCollcetion.FindAll().Documents;
            return list;
        }
        public DataSet FindAllTable()
        {
            return FindAll().ConvertToDataSet();
        }
        public DataSet FindAllTable(Document document)
        {
            return this.Find(document).ConvertToDataSet();
        }
        public DataSet FindAllTable(Document document, int index, int count, out int maxCount)
        {
            var lists = GetMongoCollcetion.Find(document);
            maxCount = lists.Documents.Count();
            return  GetMongoCollcetion.Find(document).Skip(index).Limit(count).Documents.ToList().ConvertToDataSet();
        }
        public DataSet FindAllTable(int index,int count,out int maxCount)
        {
            IQueryable<Document> query = GetMongoCollcetion.Linq<Document>();
            maxCount = query.Count();
            return query.Skip(index).Take(count).ToList().ConvertToDataSet();
        }
        /// <summary>
        /// 查找指定条件的集合
        /// </summary>
        /// <param name="document">条件</param>
        /// <returns></returns>
        public IEnumerable<Document> Find(Document document)
        {
            return GetMongoCollcetion.Find(document).Documents;
        }

        /// <summary>
        /// 查找单个记录
        /// </summary>
        /// <param name="document">条件</param>
        /// <returns>如果结果有多条 则返回最后一条记录</returns>
        public Document FindOne(Document document)
        {
            return GetMongoCollcetion.FindOne(document);
        }
    }
}
